//
//  XVCVisitorAppointmentDetail.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/13.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCVisitorAppointmentDetail.h"
#import "XTCVisitorDetail.h"
#import "ShowTipAndContentClickCell.h"
#import "XXTool.h"
#import "LiuyanViewController.h"

@interface XVCVisitorAppointmentDetail ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (nonatomic,strong) UITableView *tableview;

@property(nonatomic,strong) UIButton * liuyan;

@property(nonatomic) Boolean needFlush;


@end

@implementation XVCVisitorAppointmentDetail
{
    NSMutableArray <XMLNameAndValueModel*> *useData;
    XMLVisitorAppointDetailModel *perData;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(UIButton*)liuyan{
    if(_liuyan == nil){
        _liuyan=[[UIButton alloc]init];
        _liuyan.titleLabel.font = [XXTool systemFontOfSize:16.5f];
        _liuyan.backgroundColor=[XXTool colorWithProject];
        [_liuyan setTitle:NSLocalizedString(@"留言", nil) forState:UIControlStateNormal];
        [_liuyan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_liuyan addTarget:self action:@selector(clickLiuyanButton) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_liuyan];
    }
    return  _liuyan;
}

-(void)initParameter
{
    useData = [[NSMutableArray alloc] init];
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"预约详情", nil)];
    if (_useType==XVCVisitorAppointmentDetailIsAccepted)
    {
        
    }
    else if (_useType==XVCVisitorAppointmentDetailDelete)
    {
        [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"删除", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickCancelAppointment)]];
    }
    else if(_useType==XVCVisitorLiuyan)
    {
        [self.navigationItem setTitle:NSLocalizedString(@"留言详情", nil)];
        [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:nil]];
    }
    else
    {
        [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"取消预约", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickCancelAppointment)]];
    }
    _needFlush=true;
    [self tableview];
    [self liuyan];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(_needFlush){
        [self initData];
    }
}

/**
 * 点击留言
 */
-(void)clickLiuyanButton{
    _needFlush=true;
    if(UserInfo.shareUserInfo.isExhibitor){
        [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:self.appointmentID Type:XVCExhi];
    }else{
        [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:self.appointmentID Type:XVCVisitor];
    }}

-(void)initData
{
    _needFlush=false;
    [self.view startLoading];
    if(_useType==XVCVisitorLiuyan){
        [XMLVisitorAppointDetailModel requestVistorLiuyanDetailWithAppointmentId:0 ID:self.appointmentID completion:^(BOOL isSuccess, XMLVisitorAppointDetailModel *data,NSMutableArray<LiuyanDetailModel *>* dataList){
            if (isSuccess)
            {
                perData = data;
                useData = [[NSMutableArray alloc] init];
                //[useData removeAllObjects];
                
                for (int i=0;i<dataList.count;i++)
                {
                    XMLNameAndValueModel *one = [[XMLNameAndValueModel alloc] init];
                    if([dataList[i].mstype isEqualToString:@"1"]){
                        one.name = NSLocalizedString(@"观众留言", nil);
                    }else{
                        one.name = NSLocalizedString(@"展商留言", nil);

                    }
                    one.value = NSLocalizedString(dataList[i].content,nil);
                    [useData addObject:one];
                }
                [_tableview reloadData];
            }
            [self.view stopLoading];
        }];
    }else{
        [XMLVisitorAppointDetailModel requestVistorAppintDetailWithAppointmentId:self.appointmentID
                                                                      completion:^(BOOL isSuccess, XMLVisitorAppointDetailModel *data) {
                                                                          if (isSuccess)
                                                                          {
                                                                              perData = data;
                                                                              
                                                                              XMLNameAndValueModel *one = [[XMLNameAndValueModel alloc] init];
                                                                              one.name = NSLocalizedString(@"预约时间", nil);
                                                                              one.value = [XXTool stampToTimeWithoutYear:data.order_time];
                                                                              [useData addObject:one];
                                                                              
                                                                              XMLNameAndValueModel *two = [[XMLNameAndValueModel alloc] init];
                                                                              two.name = NSLocalizedString(@"预约留言", nil);
                                                                              two.value = data.value;
                                                                              [useData addObject:two];
                                                                              
                                                                              [_tableview reloadData];
                                                                          }
                                                                          [self.view stopLoading];
                                                                      }];
    }
    
    NSLog(@"数量==%ld",useData.count);
}

-(void)footerFresh
{
    if(_useType==XVCVisitorLiuyan){
        [XMLVisitorAppointDetailModel requestVistorLiuyanDetailWithAppointmentId:useData.count ID:self.appointmentID completion:^(BOOL isSuccess, XMLVisitorAppointDetailModel *data,NSMutableArray<LiuyanDetailModel *>* dataList){
            if (isSuccess)
            {
                perData = data;
                
                for (int i=0;i<dataList.count;i++)
                {
                    XMLNameAndValueModel *one = [[XMLNameAndValueModel alloc] init];
                    if([dataList[i].mstype isEqualToString:@"1"]){
                        one.name = NSLocalizedString(@"观众留言", nil);
                    }else{
                        one.name = NSLocalizedString(@"展商留言", nil);
                        
                    }
                    one.value = NSLocalizedString(dataList[i].content,nil);
                    [useData addObject:one];
                }
                [_tableview reloadData];
            }
            [_tableview.mj_footer endRefreshing];
        }];
    }
    else
    {
        [_tableview.mj_footer endRefreshing];
    }
}

-(void)refreshLayout:(CGRect)frame
{
    if(_useType==XVCVisitorLiuyan){
        _tableview.frame = CGRectMake(0, 0, frame.size.width
                                      , frame.size.height-45);
        _liuyan.hidden=NO;
        _liuyan.frame=CGRectMake(0, _tableview.frame.size.height, frame.size.width, 45);
    }else{
        _tableview.frame = CGRectMake(0, 0, frame.size.width
                                      , frame.size.height);
        _liuyan.hidden=YES;
    }
    
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        XTCVisitorDetail *cell = [XTCVisitorDetail cellSelectionStyleNoneWithTableView:tableView];
        [cell setCellWithData:perData personType:XVVReleasePersonInfomationExhibitor];
        return cell;
    }
    else
    {
        ShowTipAndContentClickCell *cell = [ShowTipAndContentClickCell cellSelectionStyleNoneWithTableView:tableView];
        [cell setCellWithData:useData[indexPath.row-1]];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        return [XTCVisitorDetail getHeight:perData personType:XVVReleasePersonInfomationExhibitor];
    }
    else
    {
        return [ShowTipAndContentClickCell getCellHeightWithData:useData[indexPath.row-1]];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count+1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/**
 *  点击取消预约按钮
 */
-(void)clickCancelAppointment
{
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                               message:NSLocalizedString(@"是否确定取消预约", nil)
                              delegate:self
                     cancelButtonTitle:NSLocalizedString(@"取消", nil)
                     otherButtonTitles:NSLocalizedString(@"确定", nil), nil] show];
}

#pragma mark alertViewdelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        [DKHTTPRequest sendDataWithShortUrl:@"msg_delyy"
                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=%@&passwd=%@&orderid=%@",[UserInfo shareUserInfo].userID,[UserInfo shareUserInfo].userPassword,_appointmentID]]
                                 ImageArray:nil
                                    TimeOut:10
                                 Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                     if ([[serverDic objectForKey:@"code"] intValue]==200)
                                     {
                                         if (_useType==XVCVisitorAppointmentDetailDelete)
                                         {
        
                                         }
                                         else
                                         {
                                             [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"预约取消成功", nil)];
                                         }
                                         [self.navigationController popViewControllerAnimated:YES];
                                     }
                                     return YES;
                                 } IsBuffering:NO
                          IsNeedReplaceWrap:NO
                            IsUseUpLoadHead:NO];
    }
}

#pragma mark api
+(void)pushVistorAppointmentDetailWithSourceVC:(UIViewController *)sourceVC apmentID:(NSString *)apId
{
    XVCVisitorAppointmentDetail *vc = [[XVCVisitorAppointmentDetail alloc] init];
    vc.appointmentID = apId;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

+(void)pushVistorAppointmentDetailWithSourceVC:(UIViewController*)sourceVC apmentID:(NSString*)apId useType:(XVCVisitorAppointmentDetailType)type
{
    XVCVisitorAppointmentDetail *vc = [[XVCVisitorAppointmentDetail alloc] init];
    vc.appointmentID = apId;
    vc.useType = type;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

+(void)pushVistorLiuyanDetailWithSourceVC:(UIViewController*)sourceVC apmentID:(NSString*)apId useType:(XVCVisitorAppointmentDetailType)type{
    XVCVisitorAppointmentDetail *vc = [[XVCVisitorAppointmentDetail alloc] init];
    vc.appointmentID = apId;
    vc.useType = type;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
